/**
 * 
 */
package com.nari.interfaces.decipher.gprs.protocolcodec;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.charset.Charset;
import java.util.Date;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

import com.nari.interfaces.decipher.gprs.FrameEntitySeal;
import com.nari.interfaces.decipher.gprs.vo.ClientHost;
import com.nari.interfaces.decipher.gprs.vo.DistributeFrameStateSeal;
import com.nari.platform.date.CalendarUtils;
import com.nari.platform.utils.NumberSystemConversion;

/**
 * 基于帧协议的数据编码器
 * @author Xiaopeng
 * @date 2013-11-4
 */
public class FrameProtocolEncoder extends ProtocolEncoderAdapter {
	private final Charset charset;
	
	/**
	 * 帧协议编码器构造实例
	 * @param charset
	 */
	public FrameProtocolEncoder(Charset charset) {
		this.charset = charset;
	}
	
	/**
	 * 消息编码输出
	 * 编码的方式就是按照帧协议拼装字符串到IoBuffer缓冲区
	 * @param session
	 * @param message
	 * @param encoderOutput
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-11-4
	 */
	public synchronized void encode(IoSession session, Object message, 
			ProtocolEncoderOutput encoderOutput) throws Exception {
		SocketAddress socketAddress = session.getRemoteAddress();
		InetSocketAddress inetAddress = (InetSocketAddress) socketAddress;
		String connectIP = inetAddress.getAddress().getHostAddress();
		int port = inetAddress.getPort();
		ClientHost clientHost = ClientHost.getInstance(connectIP, port);
		//消息字符编码
//		CharsetEncoder charsetEncoder = this.charset.newEncoder();
		//创建IoBuffer缓冲区，并设置为自动扩展
		IoBuffer buffer = IoBuffer.allocate(100).setAutoExpand(true);
		//获取服务器处理反馈结果
		DistributeFrameStateSeal distributeState = (DistributeFrameStateSeal) message;
		//依据服务端数据收发状态决定帧编码类型
		String transmitMessage = "";
		if(distributeState.getServerState() == DistributeFrameStateSeal.serverState_broadcasting.intValue()) { 
			//编码服务端广播响应消息
			transmitMessage = FrameEntitySeal.encodeServerFrame(distributeState.getServerState(), "", 1);
		} else if(distributeState.getServerState() == DistributeFrameStateSeal.serverState_requestData.intValue()) { 
			//编码服务端索要数据响应消息
			//集中器号
			String concentratorNo = distributeState.getConcentratorNo();
			//要求发送的帧序号
			int requestFrameIndex = distributeState.getRequestFrameIndex();
			//编码服务端响应消息
			transmitMessage = FrameEntitySeal.encodeServerFrame(distributeState.getServerState(), concentratorNo, requestFrameIndex);
		}
		System.out.println("######Response Transmit "+clientHost+"["+CalendarUtils.parseDateTimeToString(new Date())+"]######");
		System.out.println(transmitMessage);
		System.out.println("##############################################################");
		//添加发送消息
		buffer.put(NumberSystemConversion.formatHexString2ByteArray(transmitMessage, " "));
//		buffer.putString(transmitMessage, charsetEncoder);
		//用于将缓冲区的position移至0，limit移至刚才的position，为输出做准备
		buffer.flip();
		//输出IoBuffer缓冲区实例
		encoderOutput.write(buffer);
	}
	
	/**
	 * 手动释放会话编码所申请的资源
	 * @param session
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-11-4
	 */
	public void dispose(IoSession session) throws Exception {
		super.dispose(session);
	}

}
